<template>
  <el-dialog
    title="请选择商品"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :visible.sync="dialogShow"
    :before-close="close"
    class="product_dialog dialog900 small-padding-dialog"
  >
    <div class="mainor_form batch-wrap">
      <el-form
        :inline="true"
        :model="productForm"
        class="demo-form-inline"
        ref="productFrom"
        label-width="110px"
      >
        <el-form-item label="商品分类：" prop="category_id" class="wid180">
          <el-cascader
            v-model="productForm.category_id"
            :options="options"
            :props="defaultProps"
            placeholder="请选择商品分类"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="商品名称" prop="name">
          <el-input
            v-model.trim="productForm.name"
            placeholder="请输入商品名称"
            clearable
            class="wid180"
            maxlength="10" show-word-limit
          ></el-input>
        </el-form-item>
      </el-form>
      <div>
        <el-button class="reset mg-r-6" type="primary" @click="reset">重置</el-button>
        <el-button type="primary" :loading="listLoading" @click="search">搜索</el-button>
      </div>
    </div>
    <div class="main_table">
      <el-table
        ref="productTable"
        v-loading="listLoading"
        :data="tableData"
        element-loading-text="加载中"
        fit
        border
        highlight-current-row
        height="400"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" :selectable="selectable" />
        <el-table-column label="商品名称" prop="goods_name" show-overflow-tooltip/>
        <el-table-column label="单位" prop="unit_name" show-overflow-tooltip/>
        <el-table-column label="市场价" prop="market_price" show-overflow-tooltip/>
        <el-table-column label="最近一次采购价" prop="latest_purchase_price" show-overflow-tooltip/>
        <el-table-column
          label="操作"
          width="180"
        >
          <template slot-scope="scope">
            <div>
              <p class="is-selectd" @click="changeCheckbox(scope.row)" :class="scope.row.is_selected ? 'delete' : 'add'" v-if="selectable(scope.row)">{{ scope.row.is_selected ? '移出' : '添加' }}</p>
              <p class="is-selectd" v-else>-</p>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="main_page boxdq">
      <!-- 分页 -->
      <div></div>
      <div class="main_page_r">
        <MainPagination
          :current-page="currentPage"
          :page-sizes="[10, 30, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          background
          @size-change="changeLimit"
          @current-change="changePage"
        />
      </div>
    </div>

    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { getInfo } from '@/api/menu.js'
export default {
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    },
    disabledList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      // 筛选商品
      shield: false,
      productForm: {
        category_id: [],
        name: ''
      },
      defaultProps: {
        children: "selfNext",
        label: "title",
        value: "id",
        checkStrictly: true,
      },
      options: [],
      listLoading: false,
      currentPage: 1,
      total: 1,
      pageSize: 10,
      tableData: [],
    }
  },
  methods: {
    selectable(row) {
      return !this.disabledList.map(el => el.goods_spu_id).includes(row.goods_spu_id)
    },
    close() {
      this.currentPage = 1
      this.$emit('close')
    },
    getData() {
      getInfo('/common/getGoodsCategory').then(res => {
        if(res && res.data && res.data.length) {
          this.options = res.data
        }
      })
      this.getTableData()
    },
    getTableData() {
      this.listLoading = true
      let category_id =  ''
      let pid = this.productForm.category_id[0]
      if(this.productForm.category_id.length > 1) {
        category_id = this.productForm.category_id.length > 1 ? this.productForm.category_id[1] : this.productForm.category_id[0]
      }
      getInfo('/packageGoods/goodsSpuList', {
        page: this.currentPage,
        pagesize: this.pageSize,
        name: this.productForm.name,
        category_id,
        pid
      }).then(res => {
        if(res && res.data) {
          this.tableData = res.data.list
          this.total = res.data.total
          this.listLoading = false
        }
      })
    },
    reset() {
      this.$refs.productFrom.resetFields()
      this.search()
    },
    search() {
      this.currentPage = 1
      this.getTableData()
    },
    changeLimit(page) {
      this.pageSize = page
      this.getTableData()
    },
    changePage(page) {
      this.currentPage = page
      this.getTableData()
    },
    handleSelectionChange(val) {
      const list = val.map(el => el.goods_spu_id)
      this.tableData.forEach(el => {
        el.is_selected = list.includes(el.goods_spu_id)
      })
    },
    submit() {
      this.$emit('submit', this.tableData.filter(el => el.is_selected))
      this.close()
    },
    changeCheckbox(row) {
      this.$refs.productTable.toggleRowSelection(row, !row.is_selected)
    }
  },
}
</script>
<style scoped lang="scss">
.product_dialog {
  height: 835px;
  /deep/.el-dialog__header {
    border-bottom: 1px solid #EBEEF5;
  }
  /deep/.el-dialog__body {
    padding: 0 20px;
  }
  .mainor_form {
    margin-top: 12px;
  }
  /deep/.el-button--primary {
    background-color: #1BA88E;
    color: #fff;
    &:hover {
      background-color: rgba(27,168,142, .7);
      border-color: rgba(27,168,142, .7);
    }
    &.reset {
      background-color: #fff;
      border: 1px solid #03A98E;
      color: #03A98E;
      &:hover {
        background-color: rgba(27,168,142, .3);
        border-color: rgba(27,168,142, .3);
      }
    }
  }
  .is-selectd {
    cursor: pointer;
    color: rgba(0, 0, 0, .75);
    &.add{
      color: #42BEAA;
    }
    &.delete{
      color: #FF7785;
    }
  }
  /deep/.el-dialog__footer {
    border-top: 1px solid #EBEEF5;
    margin-top: 12px;
  }
}
</style>